<?php

namespace CtyunOss\config;

use Carbon\Carbon;
use CtyunOss\cors\ArrayObject;
use CtyunOss\exceptions\OssConfigException;
use CtyunOss\interfaces\OssConfigInterface;

/**
 * 天翼云OSS 配置类
 * @package    CtyunOss\config
 *
 * @author     Violet_Ice紫冰 <violetice@aliyun.com>
 * @create     2020-06-24 14:26:22
 */
class OssConfig extends ArrayObject implements OssConfigInterface
{
    /** @var string 天翼云OSS 接口ID */
    private $AccessKeyId;

    /** @var string 天翼云OSS 接口秘钥 */
    private $SecretAccessKey;

    /** @var string 天翼云OSS API Endpoint */
    private $Endpoint = 'oos-cn.ctyunapi.cn';

    /** @var string 天翼云OSS Bucket */
    private $Bucket;

    /** @var string 签名算法版本 */
    private $SignVersion = 'v2';

    /** @var bool 调试模式 */
    public $debug = false;

    /**
     * @param OssConfig $config
     *
     * @throws OssConfigException
     */
    public static function isBucket(OssConfig $config)
    {
        if (empty($config->getBucket())) {
            throw new OssConfigException('缺少 Bucket 配置');
        }
    }

    /**
     * @return string
     */
    public function getAccessKeyId()
    {
        return $this->AccessKeyId;
    }

    /**
     * @param string $AccessKeyId
     */
    public function setAccessKeyId($AccessKeyId)
    {
        $this->AccessKeyId = $AccessKeyId;
    }

    /**
     * @return string
     */
    public function getSecretAccessKey()
    {
        return $this->SecretAccessKey;
    }

    /**
     * @param string $SecretAccessKey
     */
    public function setSecretAccessKey($SecretAccessKey)
    {
        $this->SecretAccessKey = $SecretAccessKey;
    }

    /**
     * @return string
     */
    public function getEndpoint()
    {
        return $this->Endpoint;
    }

    /**
     * @param $Endpoint
     *
     * @throws OssConfigException
     */
    public function setEndpoint($Endpoint)
    {
        if (!preg_match('/^oos-([a-z]+)\.ctyunapi\.cn$/', $Endpoint, $match)) {
            throw new OssConfigException('Endpoint 不正确，请仔细核对文档');
        }

        // 目前支持的 Endpoint 时间：2020-06-24 14:26:22
        $Endpoints = [
            'cn', 'hazz', 'lnsy', 'sccd', 'xjwlmq',
            'gslz', 'sdqd', 'gzgy', 'hbwh', 'xzls',
            'ahwh', 'gdsz', 'jssz', 'sh2'
        ];
        if (!in_array($match[1], $Endpoints)) {
            throw new OssConfigException('Endpoint [' . $Endpoint . ']，不在支持的列表中');
        }

        $this->Endpoint = $Endpoint;
    }

    /**
     * @return string
     */
    public function getBucket()
    {
        return $this->Bucket;
    }

    /**
     * @param string $Bucket
     */
    public function setBucket($Bucket)
    {
        $this->Bucket = $Bucket;
    }

    /**
     * @return string
     */
    public function getSignVersion(): string
    {
        return $this->SignVersion;
    }

    /**
     * @param string $SignVersion
     *
     * @throws OssConfigException
     */
    public function setSignVersion(string $SignVersion)
    {
        if (!in_array($SignVersion, [static::SIGN_V2, static::SIGN_V4])) {
            throw new OssConfigException('不支持的签名算法');
        }

        if ($SignVersion === static::SIGN_V4) {
            throw new OssConfigException('目前不支持v4版签名算法');
        }
        $this->SignVersion = $SignVersion;
    }
}